package com.bawei.club.test

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.club.R
import com.bawei.lib_common.flow_eventbus.event.LoginEvent
import com.bawei.lib_common.flow_eventbus.observe.observeEvent
import com.bawei.lib_common.flow_eventbus.post.postEvent
import com.bawei.lib_common.flow_eventbus.util.removeStickyEvent
import com.bawei.lib_common.livedata_eventbus.LiveDataBus

/**
(1)mVersion(livedata的，只要调用setValue，值+1)
(2)mLastVersion（是observer，当onChanged接受数据，值等于mVersion）
(3)数据如何更新？
a。setValue():mVersion++->considerNotify
b。considerNotify():判断mVersion和mLastversion的值
mLastVersion >= mVersion :retrun 不执行
mLastVersion < mVersion: mLastVersion = mVersion，触发onchange方法
 */
class TestActivity : AppCompatActivity() {

    private val mViewModel:TestViewModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_test)
        mViewModel.testLiveData.observe(this){it->
            println("收到数据 $it")
        }
    }

    fun toMain(view: View) {
        ARouter.getInstance().build("/main/MainActivity").navigation()
    }

    fun postLoginEvent(view: View) {
       postEvent(LoginEvent())
    }
    fun observe(view: View) {
        observeEvent<LoginEvent>{
            println("收到登录事件")
        }
    }
    fun observeSticky(view: View) {
        observeEvent<LoginEvent>(isSticky = true){
            println("收到登录事件")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        removeStickyEvent(LoginEvent::class.java)
    }

    fun postLoginEventLiveData(view: View) {
//        LiveDataBus.with<LoginEvent>("login").value = LoginEvent()
        LiveDataBus.postSticky("login", LoginEvent())
        LiveDataBus.post("out",  LoginEvent())
    }

    fun observeLivedata(view: View) {
//        LiveDataBus.with<LoginEvent>("login").observe(this){
//            println("收到登录事件")
//        }
        LiveDataBus.observeSticky<LoginEvent>(this,"login"){
            println("收到登陆事件")
        }
        LiveDataBus.observe<LoginEvent>(this, "out"){
            println("收到退出事件")
        }
    }

    fun getData(view: View) {
        mViewModel.getData()
    }

}